package com.holidaygo.activities;
import java.util.HashMap;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.app.ListActivity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;

import com.holidaygo.db.JSONParser;
import com.holidaygo.db.MySingleton;
import com.holidaygo.imageList.LazyAdapter;
import com.holidaygo.activities.R;

public class VacRen_results extends Activity{

		private static String url_search_custom = "http://www.holidaygo.com/web_service?ajax=1&action=search_customers&";
		private static String url_customer = "http://www.holidaygo.com/web_service?ajax=1&action=get_place&id=";
		
		private static final String TAG_ID = "ID";
		public static final String TAG_NAME = "PropertyName";
		private static final String TAG_RATING = "Rating";
		private static final String TAG_UNITS="Units";
		private static final String TAG_SLEEPS="Sleeps";
		public static final String TAG_MIN_PRICE="MinPrice";
		private static final String TAG_MIN_TYPE="MinPriceType";
		public static final String TAG_PIC="MainPicture";
		public static final String tag_phone="phone";

		public static String countryValue;
		public static String regionValue;
		
		@Override
		protected void onCreate(Bundle savedInstanceState) {

			Bundle extras = getIntent().getExtras();
			// Get data via the key
			String codeValue = extras.getString("code");
			String areaValue = extras.getString("area");
			countryValue = extras.getString("country");
			regionValue = extras.getString("region");
			if (codeValue != null && areaValue!=null) {
			}

			MySingleton.getInstance().customers.clear();
			setContentView(R.layout.vacation_rentals_list);
			JSONParser jParser = new JSONParser();
			JSONArray json = jParser.getJSONfromURL(url_search_custom+areaValue+"="+codeValue);
			try {
				for(int i = 0; i < json.length(); i++){
					JSONObject c = json.getJSONObject(i);
					// Storing each json item in variable
					String id = c.getString(TAG_ID);
					String name = c.getString(TAG_NAME);
					String rating = c.getString(TAG_RATING);
					String units = c.getString(TAG_UNITS);
					String sleeps = c.getString(TAG_SLEEPS);
					String minPrice = c.getString(TAG_MIN_PRICE);
					String minType = c.getString(TAG_MIN_TYPE);
					String picture = c.getString(TAG_PIC);
					
					// creating new HashMap
					HashMap<String, String> map = new HashMap<String, String>();

					///////////////////////////////////////////////
					
					JSONParser jParserPhone = new JSONParser();
					JSONObject jsonPhone = jParserPhone.getJSONFromUrl(url_customer+id);
					String phone=jsonPhone.getJSONObject("Owner_contact_info").getString("Phone");
					//System.out.println("!!!!!!!!!!!!"+phone);
					
					//////////////////////////////////////////////
					
					// adding each child node to HashMap key => value
					map.put(TAG_ID, id);
					map.put(TAG_NAME, name);
					map.put(TAG_RATING, rating);
					map.put(TAG_UNITS, units);
					map.put(TAG_SLEEPS, sleeps);
					map.put(TAG_MIN_PRICE, minPrice);
					map.put(TAG_MIN_TYPE, minType);
					map.put(TAG_PIC, picture);
					map.put(tag_phone, phone);

					// adding HashList to ArrayList
					MySingleton.getInstance().customers.add(map);
				}
			} catch (JSONException e) {
				e.printStackTrace();
			}
			super.onCreate(savedInstanceState);
			/*
			 * Updating parsed JSON data into ListView
			 * 
			ListAdapter adapter = new SimpleAdapter(this, MySingleton.getInstance().customers,
					R.layout.vacation_rentals_item,//regionValue+"-"+countryValue    R.id.country_region
					new String[] { TAG_NAME,TAG_MIN_PRICE,"phone" }, new int[] {
							R.id.property_name, R.id.price,R.id.phone});
			
			setListAdapter(adapter); */
			
			ListView list=(ListView)findViewById(R.id.list);
			 // Getting adapter by passing xml data ArrayList
	        LazyAdapter adapter=new LazyAdapter(this, MySingleton.getInstance().customers);
	        list.setAdapter(adapter);
	 
	        // Click event for single list row
	        list.setOnItemClickListener(new android.widget.AdapterView.OnItemClickListener() {

				public void onItemClick(AdapterView<?> arg0, View arg1,
						int arg2, long arg3) {
					// TODO Auto-generated method stub
					
				}
				});
	        
		
		}

	}
